/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaz;

import Negocio.Lista_tipos;
import Negocio.Tipo;
import Negocio.Lista_usuarios;
import Negocio.TipoUsuario;
import Negocio.Usuarios;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jorge
 */
public class UsuarioInterfaz extends javax.swing.JFrame {
    Usuarios usuario = new Usuarios();
    TipoUsuario ut = new TipoUsuario();
    Tipo tipo = new Tipo();
    Lista_usuarios listaUsuarios = new Lista_usuarios();
    Lista_tipos listaTipos = new Lista_tipos();
    DefaultComboBoxModel comboTipo = new DefaultComboBoxModel();
    LoginInterfaz log;
    /**
     * Creates new form UsuarioInterfaz
     */
    public UsuarioInterfaz() {
        initComponents();
        this.setTitle("NUEVO USARIO");
    }
    
    public void listarUsuarios(){
            listaUsuarios.listar();
            ArrayList listaUs = new ArrayList();
            listaUs = listaUsuarios.getUsuarios();
            DefaultTableModel temp = (DefaultTableModel) tblUsuarios.getModel();
            for (int i = tblUsuarios.getRowCount() - 1; i >= 0; i--) {
                temp.removeRow(i);
            }
            for (int i = 0; i < listaUs.size(); i++) {
                Usuarios otroUsuario = new Usuarios();
                otroUsuario = (Usuarios) listaUs.get(i);
                usuario.setCedula(otroUsuario.getCedula());
                ut.setUsuario(usuario);                
                ut.buscar();     
                tipo.setId(ut.getTipo().getId());
                tipo.buscar();
                Object nuevo[] = {otroUsuario.getCedula(), otroUsuario.getNombre(), otroUsuario.getApellido(), tipo.getNombre()};
                temp.addRow(nuevo);            
            }
            tblUsuarios.setModel(temp);
    }
    
    public void agregarCargosCombo() {
        comboTipo.removeAllElements();
        listaTipos.listar();
        ArrayList tipos = listaTipos.getTipos();
        Iterator it = tipos.iterator();
        while (it.hasNext()) {
            Tipo t = (Tipo) it.next();
            comboTipo.addElement(t.getNombre());
        }
        cmbCargo.setModel(comboTipo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtCedulaUs = new javax.swing.JTextField();
        txtNombreUs = new javax.swing.JTextField();
        txtApellidoUs = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cmbCargo = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        txtContraseñaUs = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblUsuarios = new javax.swing.JTable();
        btnAgregarUs = new javax.swing.JButton();
        btnCancelarUs = new javax.swing.JButton();
        btnRegresarUs = new javax.swing.JButton();
        btnActualizarUs = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Nuevo Usuario");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Apellido:");

        jLabel4.setText("Cedula:");

        jLabel5.setText("Cargo:");

        cmbCargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel6.setText("Contraseña:");

        tblUsuarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Cedula", "Nombre", "Apellido", "Cargo"
            }
        ));
        jScrollPane1.setViewportView(tblUsuarios);

        btnAgregarUs.setText("Agregar");
        btnAgregarUs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarUsActionPerformed(evt);
            }
        });

        btnCancelarUs.setText("Cancelar");
        btnCancelarUs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarUsActionPerformed(evt);
            }
        });

        btnRegresarUs.setText("Regresar");
        btnRegresarUs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegresarUsActionPerformed(evt);
            }
        });

        btnActualizarUs.setText("Actualizar");
        btnActualizarUs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarUsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(280, 280, 280)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(104, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(176, 176, 176)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3)
                        .addComponent(jLabel2)
                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtCedulaUs)
                    .addComponent(txtNombreUs)
                    .addComponent(txtApellidoUs)
                    .addComponent(cmbCargo, 0, 134, Short.MAX_VALUE)
                    .addComponent(txtContraseñaUs))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btnRegresarUs, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCancelarUs, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAgregarUs, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(btnActualizarUs))
                .addGap(67, 67, 67))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtCedulaUs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNombreUs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAgregarUs))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtApellidoUs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnActualizarUs))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cmbCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelarUs))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtContraseñaUs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnRegresarUs))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarUsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarUsActionPerformed
        // TODO add your handling code here:        
        log = new LoginInterfaz();
        log.setVisible(true);
        this.setVisible(false);       
    }//GEN-LAST:event_btnCancelarUsActionPerformed

    private void btnRegresarUsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegresarUsActionPerformed
        // TODO add your handling code here:
        log = new LoginInterfaz();
        log.setVisible(true);
        this.setVisible(false);    
    }//GEN-LAST:event_btnRegresarUsActionPerformed

    private void btnActualizarUsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarUsActionPerformed
        // TODO add your handling code here:
        listarUsuarios();        
    }//GEN-LAST:event_btnActualizarUsActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        agregarCargosCombo();
    }//GEN-LAST:event_formWindowOpened

    private void btnAgregarUsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarUsActionPerformed
        // TODO add your handling code here:
        usuario.setCedula(txtCedulaUs.getText());
        usuario.setNombre(txtNombreUs.getText());
        usuario.setApellido(txtApellidoUs.getText());
        usuario.setContraseña(txtContraseñaUs.getText());
        if(comboTipo.getSelectedItem()!=null){
            String nombre = (String) comboTipo.getSelectedItem();
            tipo.setNombre(nombre);
            tipo.buscarNombre();
            ut.setUsuario(usuario);
            ut.setTipo(tipo);
            usuario.grabar();
            ut.grabar();  
        }
    }//GEN-LAST:event_btnAgregarUsActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UsuarioInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UsuarioInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UsuarioInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UsuarioInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UsuarioInterfaz().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActualizarUs;
    private javax.swing.JButton btnAgregarUs;
    private javax.swing.JButton btnCancelarUs;
    private javax.swing.JButton btnRegresarUs;
    private javax.swing.JComboBox cmbCargo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblUsuarios;
    private javax.swing.JTextField txtApellidoUs;
    private javax.swing.JTextField txtCedulaUs;
    private javax.swing.JTextField txtContraseñaUs;
    private javax.swing.JTextField txtNombreUs;
    // End of variables declaration//GEN-END:variables
}
